layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : "/userAdmin/list/",
        cellMinWidth : 95,
        height : "full-125",
        text: { none: "Aucun résultat"},
        id : "listTable",
        cols : [[
            // {field: 'reviewStatus', title: "État", align:"center", templet : function( d){
            //         switch ( d.reviewStatus) {
            //             case "PASS": return "<span style='color: green;'>审核通过<span>";
            //             case "REJECT": return "<span style='color: red;'>审核拒绝<span>";
            //             case "DEFAULT": return "<span>未审核<span>";
            //             default: return "未审核";
            //         }
            // }},
            {field: 'jobNumber', title: "公司账号", align:"center", templet : function( d){
                if ( d.idAgree == "1") {
                    return "";
                }else{
                    return d.jobNumber;
                }
            }},
            {field: 'prenom', title: "备注", align:"center"},
            {field: 'nom', title: "PRENOM", align:"center", templet : function( d){
                if( d.nomNumber){
                    return d.nom + d.nomNumber;
                }
                return d.nom;
            }},
            {title: '操作', width:600, templet:'#listBar',fixed:"right", align:"center",templet : function( d) {
                var str = "";
                if( d.reviewStatus != "PASS"){
                    str += "<a class=\"layui-btn layui-btn-xs\" lay-event=\"review\">审核</a>\n"
                }
                str += "<a class=\"layui-btn layui-btn-xs\" lay-event=\"editBase\">编辑基本资料</a>\n";
                if( d.agreementId){
                    str += "<a class=\"layui-btn layui-btn-xs\" lay-event=\"agreementInfo\">编辑合同资料</a>\n";
                }
                str += "" +
                //"<a class=\"layui-btn layui-btn-xs\" lay-event=\"editEmploye\">绑定MOONCARD</a>\n" +
                //"<a class=\"layui-btn layui-btn-xs layui-btn-danger\" lay-event=\"showLimitInfo\">查看额度信息</a>\n" +
                "<a class=\"layui-btn layui-btn-xs layui-btn-danger\" lay-event=\"mooncardCardAdmin\">银行卡号设置</a>\n" +
                "<a class=\"layui-btn layui-btn-xs layui-btn-danger\" lay-event=\"resetPassword\">重置密码</a>\n";

           //     "<a class=\"layui-btn layui-btn-xs layui-btn-danger\" lay-event=\"del\">删除</a>"
                return str;
            }}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.length, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
    });

    //获取浏览器最大高度，以防页面显示不完全
    var maxHeight = $(window).height();

    //添加/修改/详细查看（readonly=true）
    function showReview( data, readonly){
        var index = layui.layer.open({
            title : "公司审核",
            type : 2,
            area: [650 + 'px', (maxHeight < 390 ? maxHeight - 10 : 390) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "USER_REVIEW", //设定一个id，防止重复弹出
            content : "reviewFormData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "#roleSelect").val( data.role.id);
                    body.find( "#roleIdForValue").val( data.role.id);
                    body.find( "#roleSelect").attr( "readonly", readonly);

                    body.find( "input[name=jobNumber]").val( data.jobNumber).attr( "readonly", readonly);
                    body.find( "input[name=bankCard1]").val( data.bankCard1).attr( "readonly", readonly);
                    body.find( "input[name=bankCard2]").val( data.bankCard2).attr( "readonly", readonly);
                    body.find( "input[name=bankCard3]").val( data.bankCard3).attr( "readonly", readonly);
                    body.find( "input[name=bankCard4]").val( data.bankCard4).attr( "readonly", readonly);

                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                }
                form.render();
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加/修改/详细查看（readonly=true）
    function showBaseInfo( data, readonly){
        console.log('showBaseInfo')
        var index = layui.layer.open({
            title : "基础信息编辑",
            type : 2,
            area: [600 + 'px', (maxHeight < 400 ? maxHeight-10 : 400) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id:  "userAdmin_ADD", //设定一个id，防止重复弹出
            content : "baseFormData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    // body.find( "input[name=phone]").val( data.phone).attr( "readonly", readonly);
                    body.find( "input[name=nom]").val( data.nom).attr( "readonly", readonly);
                    // body.find( "input[name=prenom]").val( data.prenom).attr( "readonly", readonly);
                    // body.find( "input[name=email]").val( data.email).attr( "readonly", readonly);

                    body.find( "input[name=jobNumber]").val( data.jobNumber).attr( "readonly", readonly);
                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                }
                form.render();
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加/修改/详细查看（readonly=true）
    function showAgreement( data, readonly){
        var index = layui.layer.open({
            title : "合同信息编辑",
            type : 2,
            area: [900 + 'px', (maxHeight < 600 ? maxHeight-10 : 600) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id:  "userAdmin_ADD", //设定一个id，防止重复弹出
            content : "/pages/agreement/agreementFormData.html?agreementType=" + data.agreementType + "&agreementId=" + data.agreementId + "&readonly=" + readonly,
            success : function(){
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //添加/修改/详细查看（readonly=true）
    function showEmployeInfo( data, readonly){
        var index = layui.layer.open({
            title : "公司信息编辑",
            type : 2,
            area: [650 + 'px', (maxHeight < 600 ? maxHeight - 10 : 600) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id:  "userAdmin_ADD", //设定一个id，防止重复弹出
            content : "employeFormData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "#roleSelect").val( data.role.id);
                    body.find( "#roleIdForValue").val( data.role.id);
                    body.find( "#roleSelect").attr( "disabled", true);

                    body.find( "input[name=jobNumber]").val( data.jobNumber).attr( "readonly", true);
                    body.find( "input[name=bankCard1]").val( data.bankCard1).attr( "readonly", readonly);
                    body.find( "input[name=bankCard2]").val( data.bankCard2).attr( "readonly", readonly);
                    body.find( "input[name=bankCard3]").val( data.bankCard3).attr( "readonly", readonly);
                    body.find( "input[name=bankCard4]").val( data.bankCard4).attr( "readonly", readonly);

                    if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                }
                form.render();
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //重置密码
    function resetPassword( data){
        layer.confirm('确定重置该用户的密码？将会为该用户重新生成一组随机密码', {icon: 3, title: '提示信息'}, function (index) {
            $.post( "/userAdmin/resetPassword",{
                id : data.id
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.alert( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //mooncard银行卡管理
    function mooncardCardAdmin( data){
        var index = layui.layer.open({
            title : "公司[" + data.jobNumber + "]Mooncard卡号设置",
            type : 2,
            area: [550 + 'px', (maxHeight < 500 ? maxHeight - 10 : 500) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id:  "userBalanceInfo", //设定一个id，防止重复弹出
            content : "mooncardCardList.html?adminUserId=" + data.id,
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                form.render();
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //查看额度信息
    function showLimitInfo( data){
        var mooncard = data.mooncard;
        if( !mooncard){
            top.layer.msg( "额度信息尚未计算，请先点击计算额度");
        }
        var index = layui.layer.open({
            title : "公司额度信息",
            type : 2,
            area: [650 + 'px', (maxHeight < 400 ? maxHeight - 10 : 400) + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id:  "userBalanceInfo", //设定一个id，防止重复弹出
            content : "userBalanceInfo.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                body.find( "input[name=validBalance]").val( mooncard.validBalance);
                body.find( "input[name=thresholdMonthly]").val( mooncard.thresholdMonthly);
                body.find( "input[name=thresholdWeekly]").val( mooncard.thresholdWeekly);
                body.find( "input[name=thresholdDaily]").val( mooncard.thresholdDaily);
                form.render();
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //删除
    function del( ids){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post( "/userAdmin/del",{
                "ids" : ids
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //添加
    $(".add_btn").click(function(){
        showBaseInfo( null, false);
    });

    //添加
    $(".handleLimitCount_btn").click(function(){
        //弹出loading
        var loadingIndex = layer.msg('后台计算中，请稍候',{icon: 16,time:false,shade:0.8});
        $.get( "/userAdmin/handleLimitCount",function( res){
            if( res.code == 200){
                tableIns.reload();
                layer.close( loadingIndex);
                top.layer.msg( res.msg);
            }else{
                top.layer.close( index);
                top.layer.msg( res.msg);
            }
        });
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus( 'listTable'),
            data = checkStatus.data, ids = "";
        if( data.length > 0) {
            for ( var i=0 ; i<data.length ; i++) {
                ids += data[i].id;
                if( i != data.length-1){
                    ids += ",";
                }
            }
            del( ids);
        }else{
            layer.msg( "请选择需要删除的数据项");
        }
    });

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'review'){ //编辑
            if( !baseConfig.checkPrivalege( baseConfig.ADMINUSER_REVIEW)){
                top.layer.msg( "只有超级管理员和一级管理员有权限审核");
                return;
            }
            showReview( data, false);
        }else if(layEvent === 'editBase'){ //编辑 基本资料
            showBaseInfo( data, false);
        }else if(layEvent === 'agreementInfo'){ //查看合同资料
            showAgreement( data, false);
        }else if(layEvent === 'editEmploye'){ //编辑 公司工号信息
            showEmployeInfo( data, false);
        }else if(layEvent === 'del'){ //删除
            del( data.id);
        }else if(layEvent === 'resetPassword'){
            resetPassword( data);
        }else if(layEvent === 'mooncardCardAdmin'){
            mooncardCardAdmin( data);
        }else if(layEvent === 'showLimitInfo'){
            showLimitInfo( data);
        }
    });
});
